package com.ffws.forestInfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ffws.base.exception.FFWSException;
import com.ffws.base.model.Label;
import com.ffws.base.model.RestResponse;
import com.ffws.feign.clients.ContentClient;
import com.ffws.feign.po.Person;
import com.ffws.forestInfo.mapper.PlanDetailMapper;
import com.ffws.forestInfo.mapper.PlanInformationMapper;
import com.ffws.forestInfo.mapper.TaskInformationMapper;
import com.ffws.forestInfo.mapper.TaskStatisticsMapper;
import com.ffws.forestInfo.model.dto.PlanInformationDto;
import com.ffws.forestInfo.model.dto.TaskInformationDto;
import com.ffws.forestInfo.model.po.PlanDetail;
import com.ffws.forestInfo.model.po.PlanInformation;
import com.ffws.forestInfo.model.po.TaskInformation;
import com.ffws.forestInfo.model.po.TaskStatistics;
import com.ffws.forestInfo.service.ForestInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author JC
 * @description 护林寻视
 * @Date 2023-8-30 15:21
 * @Version 1.0
 */
@Service
@Slf4j
public class ForestInfoServiceImpl implements ForestInfoService {

    @Resource
    PlanInformationMapper planInformationMapper;

    @Resource
    TaskInformationMapper taskInformationMapper;

    @Resource
    TaskStatisticsMapper taskStatisticsMapper;

    @Resource
    PlanDetailMapper planDetailMapper;

    @Resource
    ContentClient contentClient;

    @Override
    public RestResponse<PlanInformationDto> getPlanInfo(Long tabId) {
        PlanInformation planInformation = planInformationMapper.selectOne(new LambdaUpdateWrapper<PlanInformation>()
                .eq(PlanInformation::getTabId, tabId));
        if (planInformation == null) {
            FFWSException.cast("未找到对应的计划信息");
            return RestResponse.validfail("未找到对应的计划信息");
        }
        PlanInformationDto planInformationDto = new PlanInformationDto();

        BeanUtils.copyProperties(planInformation, planInformationDto);
        Long peopleId = planInformationDto.getPeopleId();
        if (peopleId != null) {
            Person people = contentClient.getPersonById(peopleId);
            planInformationDto.setPeopleName(people.getName());
        }
        Long inspectorsId = planInformationDto.getInspectorsId();
        if (inspectorsId != null) {
            Person inspectors = contentClient.getPersonById(inspectorsId);
            planInformationDto.setInspectorsName(inspectors.getName());
        }
        return RestResponse.success(planInformationDto);
    }

    @Override
    public RestResponse<List<TaskInformationDto>> getTaskInfo(Long planId) {
        List<TaskInformation> taskInformationList = taskInformationMapper.selectList(new LambdaUpdateWrapper<TaskInformation>()
                .eq(TaskInformation::getPlanId, planId));
        if (taskInformationList.size() <= 0) {
            return RestResponse.validfail("未找到对应的任务信息");
        }
        List<TaskInformationDto> list = new ArrayList<>();
        taskInformationList.forEach(task -> {
            TaskInformationDto taskInformationDto = new TaskInformationDto();
            BeanUtils.copyProperties(task, taskInformationDto);
            taskInformationDto.setInspectorsName(contentClient.getPersonById(task.getInspectorsId()).getName());
            taskInformationDto.setPlanName(planInformationMapper.selectOne(new LambdaQueryWrapper<PlanInformation>()
                    .eq(PlanInformation::getId, planId)).getName());
            list.add(taskInformationDto);
        });
        return RestResponse.success(list);
    }

    @Override
    public RestResponse<List<Label>> getTaskStat(Long planId) {
        TaskStatistics taskStatistics = taskStatisticsMapper.selectOne(new LambdaUpdateWrapper<TaskStatistics>()
                .eq(TaskStatistics::getPlanId, planId));
        List<Label> list = new ArrayList<>();
        if (taskStatistics == null) {
            FFWSException.cast("未找到对应的任务统计");
            return RestResponse.validfail("未找到对应的任务统计");
        }
        list.add(new Label(taskStatistics.getCompleted(), "已完成"));
        list.add(new Label(taskStatistics.getInProgress(), "未完成"));
        list.add(new Label(taskStatistics.getToBeContinued(), "进行中"));
        return RestResponse.success(list);
    }

    @Override
    public RestResponse<List<PlanDetail>> getPlanDetail(Long planId) {
        List<PlanDetail> planDetailList = planDetailMapper.selectList(new LambdaUpdateWrapper<PlanDetail>()
                .eq(PlanDetail::getPlanId, planId));
        if (planDetailList.size() <= 0) {
            RestResponse.validfail("未找到对应的巡查详情");
        }
        return RestResponse.success(planDetailList);
    }

}
